package net.caixiaomi.info.ui.football.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.baidu.mobstat.StatService;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.MatchDetailAnalysisAdapter;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.MatchInfoForTeam;
import net.caixiaomi.info.model.MatchIntroduceModel;
import net.caixiaomi.info.model.MatchTeamFutureInfo;
import net.caixiaomi.info.model.MatchTeamInfo;
import net.caixiaomi.info.util.Constants;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AnalysisFragment extends BaseFragment implements ComonFragmentImpl {
    @BindView(android.R.id.list)
    RecyclerView mListView;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;
    private MatchDetailAnalysisAdapter mAdapter;

    @Override
    protected int fragmentLayout() {
        return R.layout.frag_list;
    }


    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mProgress.setVisibility(View.GONE);
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new MatchDetailAnalysisAdapter(null);
        mListView.setAdapter(mAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        StatService.onPageStart(getActivity(), "saishifenxi-view");
    }

    @Override
    public void onPause() {
        super.onPause();
        StatService.onPageEnd(getActivity(), "saishifenxi-view");
    }

    public void setData(MatchIntroduceModel data) {
        try {
            MatchInfoForTeam matchInfo = data.getMatchInfo();
            mAdapter.setHostAndGuest(matchInfo.getHomeTeamAbbr(), matchInfo.getVisitingTeamAbbr());

            StringBuilder stringBuilder = new StringBuilder();


            List<MultiItemEntity> lists = new ArrayList<>();
            MatchTeamInfo hvMatchTeamInfo = (MatchTeamInfo) data.getHvMatchTeamInfo().clone();//历史交锋
            hvMatchTeamInfo.setType(Constants.ITEM_VIEW_HEADER1);
            lists.add(hvMatchTeamInfo);

            hvMatchTeamInfo = data.getHvMatchTeamInfo();
            hvMatchTeamInfo.setTeamAbbr("");
            lists.add(hvMatchTeamInfo);
            lists.addAll(hvMatchTeamInfo.getMatchInfos());

            MatchTeamInfo hMatchTeamInfo = data.gethMatchTeamInfo();
            stringBuilder.append(hMatchTeamInfo.getTeamAbbr()).append("(").append(getString(R.string.C_HOST)).append(")");
            hMatchTeamInfo.setTeamAbbr(stringBuilder.toString());
            stringBuilder.setLength(0);

            lists.add(hMatchTeamInfo);
            for (MatchInfoForTeam matchInfoForTeam : hMatchTeamInfo.getMatchInfos()) {
                matchInfoForTeam.setIndex(1);
            }
            lists.addAll(hMatchTeamInfo.getMatchInfos());

            MatchTeamInfo vMatchTeamInfo = data.getvMatchTeamInfo();
            stringBuilder.append(vMatchTeamInfo.getTeamAbbr()).append("(").append(getString(R.string.C_GUEST)).append(")");
            vMatchTeamInfo.setTeamAbbr(stringBuilder.toString());
            stringBuilder.setLength(0);

            lists.add(vMatchTeamInfo);
            for (MatchInfoForTeam matchInfoForTeam : vMatchTeamInfo.getMatchInfos()) {
                matchInfoForTeam.setIndex(2);
            }
            lists.addAll(vMatchTeamInfo.getMatchInfos());

            data.getHomeTeamScoreInfo().setHost(true);

            lists.add(data.getHomeTeamScoreInfo());
            lists.add(data.getVisitingTeamScoreInfo());

            if(data.gethFutureMatchInfos()!=null&&data.gethFutureMatchInfos().size()>0) {
                MatchTeamFutureInfo info=new MatchTeamFutureInfo();
                info.setHost(true);
                info.setType(Constants.ITEM_VIEW_FOOTER2);
                lists.add(info);
                for(int i=0;i<data.gethFutureMatchInfos().size();i++) {
                    data.gethFutureMatchInfos().get(i).setHost(true);
                }
                lists.addAll(data.gethFutureMatchInfos());
            }else{
                MatchTeamFutureInfo info=new MatchTeamFutureInfo();
                info.setHost(true);
                info.setNoData(true);
                info.setType(Constants.ITEM_VIEW_FOOTER2);
                lists.add(info);
            }

            if(data.getvFutureMatchInfos()!=null&&data.getvFutureMatchInfos().size()>0) {
                MatchTeamFutureInfo info=new MatchTeamFutureInfo();
                info.setHost(false);
                info.setType(Constants.ITEM_VIEW_FOOTER2);
                lists.add(info);

                for(int i=0;i<data.getvFutureMatchInfos().size();i++) {
                    data.getvFutureMatchInfos().get(i).setHost(false);
                }
                lists.addAll(data.getvFutureMatchInfos());
            }else{
                MatchTeamFutureInfo info=new MatchTeamFutureInfo();
                info.setHost(false);
                info.setNoData(true);
                info.setType(Constants.ITEM_VIEW_FOOTER2);
                lists.add(info);
            }


            mAdapter.setNewData(lists);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }
}
